package com.app.controller;

import com.app.common.AreasTool;
import com.app.common.SalaryTool;
import com.app.common.WelfareTool;
import com.app.model.CustomerModel;
import com.app.model.JobIntentionModel;
import com.app.model.UserModel;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@RestController
@NoArgsConstructor
@AllArgsConstructor
@Data
@Controller
public class JobIntentionController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private JobIntentionService jobIntentionService;
    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    MsgVerifyService msgVerifyService;
    @Autowired
    JobPublishService jobPublishService;

    //增/改
    @PostMapping("/addOrupIntention")
    public Result<Object> addIntention(@RequestParam("post") int post,
//                          @RequestParam("regProvince") String regProvince,
//                          @RequestParam("regCity") String regCity,
                               @RequestParam("regArea") String regArea,
                               @RequestParam("salaryMin") Integer salaryMin,
                               @RequestParam("salaryMax") Integer salaryMax,
                               @RequestParam("recruitType") int recruitType,
                               @RequestParam("id") int id,
                               HttpServletRequest request
                          ){
        JSONObject json = new JSONObject();
        JobIntentionModel jobIntention = new JobIntentionModel();
        jobIntention.setPost(post);
        String str[]=regArea.split(",");
        jobIntention.setRegProvince(str[0]);
        jobIntention.setRegCity(str[1]);
        jobIntention.setRegArea(str[2]);
        jobIntention.setRecruitType(recruitType);
        jobIntention.setSalaryMax(salaryMax);
        jobIntention.setSalaryMin(salaryMin);
        String token = request.getHeader("token");
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token不存在");
        }
        UserModel user = userService.getUserByMobile(mobile);
        if(user==null)
        {
            return Result.error(405, "用户不存在");
        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());

        jobIntention.setCustomerId(cus.getId());
        try {
             if(id>0)
            {
                jobIntention.setId(id);
                jobIntention.setUpTime(CommonHelp.getNowTimestamp());
                jobIntentionService.update(jobIntention);
            }else {
                jobIntention.setCrtTime(CommonHelp.getNowTimestamp());
                 jobIntentionService.insert(jobIntention);
                 UserModel userModel = userService.getUserByRId(jobPublishService.findJobByPost(post).getRecId());
                 //求职意向新增后发短信
//                 int code = msgVerifyService.smsReminder(userModel.getMobile(),2);
//                 if (code == 200) {
//                     return Result.success("短信已发送，请1分钟后再试");
//                 } else {
//                     return Result.error(code, "操作失败");
//                 }
            }

        }catch (Exception e)
        {
            System.out.println(e);
        }

        return Result.success("操作成功");
    }

    //删除
    @PostMapping("/delIntention")
    public Result<Object> delIntention(@RequestParam("id") int id,
                               HttpServletRequest request){
        JSONObject json = new JSONObject();
        try {
            String token = request.getHeader("token");
            String mobile = TokenUtils.nothingness(token);
            if (mobile == null) {
                return Result.error(405, "token不存在");
            }
            UserModel user = userService.getUserByMobile(mobile);
            if(user==null)
            {
                return Result.error(405, "用户不存在");
            }

            CustomerModel cus = customerService.getCustomerById(user.getCId());
            if(id>0)
            {
                jobIntentionService.delete(id);
            }else {
               return Result.error(500,"请输入ID");
            }

        }catch (Exception e)
        {
            System.out.println(e);
        }
        return Result.success(null);
    }


    //根据用户ID查询求职意向
    @GetMapping("/getIntentionList")
    public Result<Object> getIntentionList(HttpServletRequest request) {
        String token = request.getHeader("token");
        String id = request.getParameter("id");
        Integer intentId = null;
        if(id!=null&&id!=""&&!id.equals("0"))
        {
            intentId = Integer.valueOf(id);
        }
       return Result.success(jobIntentionService.getIntentionList(token,intentId));
    }

    //根据ID查询求职意向
    @GetMapping("/getIntention")
    public Result<Object> getIntention(HttpServletRequest request) {
        String id = request.getParameter("id");
        Integer intentId = null;
        if(id!=null&&id!="")
        {
            intentId = Integer.valueOf(id);
        }
       return Result.success(jobIntentionService.getIntention(intentId));
    }

    /**
     *
     * 查询薪资
     */
    @GetMapping("/getSalaryList")
    public Result<Object> getSalaryList() {
        return Result.success(SalaryTool.SALARY_VO_LIST);
    }

    /**
     *
     * 查询公司福利
     */
    @GetMapping("/getWelfareList")
    public Result<Object> getWelfareList() {
        return Result.success(WelfareTool.SKILL_VO_LIST);
    }
}
